#include "BMI160.h"
#include "mf_i2c.h"
#include "mf_uart.h"
#include "log.h"

const static char *TAG = "bmi160.c";

FL_ErrorStatus bmi160_init(void)
{
    bool ret = FL_PASS;
    uint8_t buff;
    ret |= mf_i2c_master_read_from_device(BMI160_ADDRESS, BMI160_CHIP_ID_REG, I2C_REG_BYTE_ADDR, &buff, 1);

    if (buff == BMI160_CHIP_ID_DEFAULT)
    {
#if BMI160_DEBUG
        MY_LOGI(TAG, "CHIP_ID ok. [ret:%d]\r\n", ret);
#endif
        ret = FL_PASS;
    }
    else
    {
#if BMI160_DEBUG
        MY_LOGI(TAG, "CHIP_ID error. [ret:%d]\r\n", ret);
#endif
    }
    
    return ret;
}

FL_ErrorStatus bmi160_get_acc(bmi160_acc_t *acc_param)
{
    bool ret = FL_PASS;
    uint8_t buff = BMI160_MODE_ACC_NOR;

    ret |= mf_i2c_master_write_to_device(BMI160_ADDRESS, BMI160_CMD_REG, I2C_REG_BYTE_ADDR, &buff, 1); /* set bmi160 to acc_normal mode */
    FL_DelayUs(5);
    ret |= mf_i2c_master_read_from_device(BMI160_ADDRESS, 0x12, I2C_REG_BYTE_ADDR, &buff, 1); /* set bmi160 to acc_normal mode */
    MY_LOGI(TAG, "acc_x_7_0 -> %d. [ret:%d]\r\n", buff, ret);

    return ret;
}

FL_ErrorStatus bmi160_get_gyr(bmi160_gyr_t *gyr_param)
{
    bool ret = FL_PASS;

    return ret;
}